package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.service.WxCouponService;
import com.cskaoyan.market.util.AdminResponseCode;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author XuFeiLong
 * @DATE: 2023/4/4
 * @TIME: 0:11
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService couponService;

    @GetMapping("list")
    public Object list(Integer page, Integer limit) {
        List<MarketCoupon> couponList = couponService.list(page, limit);
        return ResponseUtil.okList(couponList);

    }

    @GetMapping("mylist")
    public Object list(short status,Integer page, Integer limit) {
        List<MarketCoupon> couponList = couponService.mylist(status,page, limit);
        return ResponseUtil.okList(couponList);

    }

    @GetMapping("selectlist")
    public Object selectlist(Integer cartId) {
        List<MarketCoupon> couponList = couponService.selectlist(cartId);
        return ResponseUtil.okList(couponList);
    }

    @PostMapping("receive")
    public Object receive(@RequestBody String json,HttpSession session){
        Integer userId = (Integer) session.getAttribute("id");
        Integer couponId = JacksonUtil.parseInteger(json, "couponId");
        int code =couponService.receive(userId,couponId);
        if(code==200){
            return ResponseUtil.ok();
        }
        if (code== AdminResponseCode.COUPON_EXISTED){
            return ResponseUtil.fail(740,"优惠券已经领取过");
        }
        return ResponseUtil.badArgument();
    }

    @PostMapping("exchange")
    public Object exchange(@RequestBody String json,HttpSession session){
        Integer userId = (Integer) session.getAttribute("id");
        String code = JacksonUtil.parseString(json, "code");
        int code1 =couponService.exchange(userId,code);
        if(code1==200){
            return ResponseUtil.ok();
        }
        if (code1== AdminResponseCode.COUPON_CODE_ERROR){
            return ResponseUtil.fail(742,"优惠券不正确");
        }else if(code1==AdminResponseCode.COUPON_USED){
            return ResponseUtil.fail(793,"优惠券已使用");

        }
        return ResponseUtil.badArgument();
    }

}
